home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1997 February
/
EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso
/
enigma
/
onlypc
/
java
/
samples.bin
/
Client.java
< prev
next >
Wrap
Text File
|
1996-09-04
|
6KB
|
225 lines
/*
* Title: Conversing Applets
* Type: Applet
* Source: Client.java
* Application Description:
* The Conversing Applets project provides a simple and effective way
* to allow applets to communicate within the same "environment", i.e.
* within the same browser page.
*
* This is done using a Mediator class as the background manager and
* other visible classes which display results. The Mediator class uses
* static variables to keep track of all the events taking place in
* these visible applets.
*
* In this example, two visible applets are used along with the one
* Mediator class. The Developer and Client class generate text strings which
* represent conversation. The Mediator object(s) take those text
* strings and distribute them to the other applet (Developer to Client and
* Client to Developer).
*
* The result is two distinct applet objects transmitting data back and
* forth. This sort of project can be very useful in developing more
* interesting Web pages as well as Web page applets which gather
* information about usage, etc. The Mediator class concept can be
* built upon to meet the needs of more specific Java-based Web
* applications.
*
* Symantec Corporation.
*/
import java.applet.Applet;
import java.awt.*;
/**
* The Client applet class
*/
public class Client extends Applet implements Runnable
{
Mediator myMediator; //Local copy of the Mediator object
String label, response; //Various applet text variables
int theNumber; //Used to track a random number
Thread clientThread; //The main applet thread
/**
* Set up the applet layout and initialize variables
*/
public void init()
{
myMediator = new Mediator();
setLayout(new GridLayout(5,1));
//{{INIT_CONTROLS
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
addNotify();
resize(189,128);
top = new java.awt.Label("I'm a Client");
top.reshape(19,20,67,13);
add(top);
client = new java.awt.Label("Client says:");
client.reshape(19,33,70,13);
add(client);
clientTalk = new java.awt.TextField(18);
clientTalk.reshape(19,46,150,18);
add(clientTalk);
developer = new java.awt.Label("The Developer just said:");
developer.reshape(19,70,108,15);
add(developer);
developerTalk = new java.awt.TextField(17);
developerTalk.reshape(19,90,148,18);
add(developerTalk);
//}}
developerTalk.setEditable(false);
clientTalk.setEditable(false);
clientTalk.setForeground(Color.white);
}
/**
* Start the main thread
*/
public void start()
{
clientThread = new Thread(this);
clientThread.start();
}
/**
* Handle any events here
*/
public boolean handleEvent(Event evt)
{
if(evt.target instanceof Button)
{
return true;
}
return false;
}
/**
* The applet action takes place here. The applet waits, gets a
* random number from the Mediator, uses that to id a piece of
* conversation and then updates the UI elements.
*/
public void run()
{
Thread clientThread = Thread.currentThread();
while(true)
{
try
{
clientThread.sleep(2000);
} catch(Exception e)
{
System.out.println(e);
}
//Keep a copy of the last random number
myMediator.oldNumber = myMediator.theNumber;
myMediator.theNumber = myMediator.genRandom();
checkWithMediator();
takeAction();
//System.out.println("response =" + response);
}
}
/**
* A helper function which uses the new Mediator random number
* to set the conversation text
*/
public synchronized void checkWithMediator()
{
int index = myMediator.theNumber;
System.out.println(index);
if(index < 10)
{
label = "Isn't Café great?";
} else if((index <= 20) && (index >= 10))
{
label = "The Café compiler is sooo FAST!";
} else if((index <= 30) && (index > 20))
{
label = "Check out the snazzy class editor.";
} else if((index <= 40) && (index > 30))
{
label = "Debugging in Café is easy.";
} else if((index <= 50) && (index > 40))
{
label = "Café is Great!";
} else if((index <= 60) && (index > 50))
{
label = "What do you mean by that?";
} else if((index <= 70) && (index > 60))
{
label = "I want to make sure you use Café.";
} else if((index <= 80) && (index > 70))
{
label = "Isn't that the truth";
} else if((index <= 90) && (index > 80))
{
label = "Wow, really!!!";
} else if((index <= 100) && (index >90))
{
label = "That's very cool";
}
//Let the Mediator know what the client said
myMediator.setClientTalk(label);
//Find out what the developer said
response = myMediator.getDeveloperTalk();
}
/**
* A helper method which updates the UI elements
*/
public void takeAction()
{
clientTalk.setText(label);
developerTalk.setText(response);
}
//{{DECLARE_CONTROLS
java.awt.Label top;
java.awt.Label client;
java.awt.TextField clientTalk;
java.awt.Label developer;
java.awt.TextField developerTalk;
//}}
}